#!/bin/bash

# 设置apt源
codename=$(lsb_release -cs)
ali="http://mirrors.aliyun.com/ubuntu/"
tsinghua="https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu"

echo -e "change apt source\n0 no change\n1 select ali (default)\n2 select tsinghua"
read -p "chosen:" option

if [[ -z "$option" || $option -eq 1 ]]
then
    option=1
    url=$ali
elif [[ $option -eq 2 ]]
then
    url=$tsinghua
fi

if [[ $option != 0 ]]
cp /etc/apt/sources.list /etc/apt/sources.list.bak

aptSource="deb $url $codename main restricted universe multiverse\n\
deb-src $url $codename main restricted universe multiverse\n\n\
deb $url $codename-security main restricted universe multiverse\n\
deb-src $url $codename-security main restricted universe multiverse\n\n\
deb $url $codename-updates main restricted universe multiverse\n\
deb-src $url $codename-updates main restricted universe multiverse\n\n\
deb $url $codename-proposed main restricted universe multiverse\n\
deb-src $url $codename-proposed main restricted universe multiverse\n\n"
then
  if [[ "$codename" = "bionic" ]] #ubuntu 18
  then
    str="deb $url $codename-backports main restricted universe multiverse\n\
deb-src $url $codename-backports main restricted universe multiverse"
    echo -e $aptSource$str > /etc/apt/sources.list
  elif [[ "$codename" = "xenial" ]] #ubuntu 16
  then
    echo -e $aptSource > /etc/apt/sources.list
  fi
  apt-get update
fi


# 设置python pip源
mkdir ~/.pip
tee ~/.pip/pip.conf <<EOF
[global]
index-url = https://pypi.mirrors.ustc.edu.cn/simple/
extra-index-url = https://pypi.mirrors.ustc.edu.cn/simple/
    https://mirrors.aliyun.com/pypi/simple/
    https://pypi.tuna.tsinghua.edu.cn/simple/
    http://pypi.mirrors.ustc.edu.cn/simple/
    https://pypi.org/simple/
trusted-host = pypi.mirrors.ustc.edu.cn
    pypi.mirrors.ustc.edu.cn
    mirrors.aliyun.com
    pypi.tuna.tsinghua.edu.cn
    pypi.mirrors.ustc.edu.cn
    pypi.org
EOF



# 设置anaconda源
tee ~/.condarc <<-'EOF'
channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
EOF
conda clean -i